package io.renren.data;

import cn.hutool.core.io.FileUtil;
import io.renren.RenrenApplication;
import io.renren.modules.avanotes.dao.SynonymTokensDao;
import io.renren.modules.avanotes.entity.SynonymTokensEntity;
import io.renren.modules.avanotes.service.SynonymTokensService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: xiaomi
 * @date: 2022/2/16
 * @description: 根据 Code:只有 = 之后才是同义，而@ 表示单独，# 表示同类。
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RenrenApplication.class)
@Slf4j
public class SynonymImport {

    @Autowired
    SynonymTokensDao stDao;

    @Autowired
    SynonymTokensService stService;

    @Test
    public void importHGDExtendsData() {
        List<SynonymTokensEntity> ll = new ArrayList<>();
        String sourcePath = "D:\\File\\词典\\哈工大信息检索研究中心同义词词林扩展版  HIT-SCIR Tongyici Cilin (Extended)（很厉害！）\\哈工大信息检索研究中心同义词词林扩展版-UTF-8.txt";
        File file = new File(sourcePath);
        //分类Code + 词（,分割）
        List<String> strings = FileUtil.readLines(file, Charset.defaultCharset());
        for (String string : strings) {
            if (string.indexOf("=") < 0) {
                continue;
            }
            //匹配到=号
            int index = string.indexOf("=");
            String code = string.substring(0, index);
            String postStr = string.substring(index + 1);
            String tokens = postStr.trim().replaceAll("\\s", ",");
            ll.add(new SynonymTokensEntity(code, tokens));
        }
        //batch insert
        stService.saveBatch(ll);
    }
}
